layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#bookList',
        url : 'list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 5,
        limits : [10,15,20,25],
        id : "bookListTable",
        cols : [[
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'bookName', title: '分类名称', align:"left"},
            {field: 'author', title: '作者', align:"left"},
            {field: 'publishDate', title: '发行时间', align:"center"},
            {field: 'localPath', title: '资源位置', align:'left'},
            {field: 'addDate', title: '创建时间', align:'center'},
            {title: '操作', width:150, templet:'#bookListBar',fixed:"right",align:"center"}
        ]]
    });

    //添加文章
    function addBook(){
        var index = layui.layer.open({
            title : "添加书籍",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area : ["400px","450px"],
            content : "bookAdd",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }

    $(".addBook_btn").click(function(){
        addBook();
    })

    //列表操作
    table.on('tool(bookList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'del') {
            //删除
            layer.confirm('确定删除此书籍？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("delete", {
                    id: data.id  //将需要删除的newsId作为参数传入
                }, function (data) {
                    data = JSON.parse(data)
                    if (data.flag === "success") {
                        layer.msg("删除成功");
                        tableIns.reload();
                        layer.close(index);
                    } else if (data.flag === "permissions") {
                        layer.msg("权限不足，无法删除！", {icon: 5});
                    } else {
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });
        } else if(layEvent === 'cover'){
            var index = layui.layer.open({
                title : "设置封面",
                type : 2,
                shadeClose: false,           //弹出框之外的地方是否可以点击
                area : ["400px","350px"],
                content : "bookAdd",
                content : "bookCover?id="+data.id,
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                }
            })
        }
    });

})